package action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Session;
import org.hibernate.Transaction;

import classes.Produit;

import sessionfactory.HibernateSessionFactory;

public class AddProductAction extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception 
			{
				Session session = HibernateSessionFactory.getSessionFactory().openSession();
				Transaction tx = session.beginTransaction();
				
				Produit toadd = new Produit ();
				toadd.setNom(request.getParameter("nom"));
				toadd.setCodeBarre(request.getParameter("codeBarre"));
				toadd.setMarge(Integer.valueOf(request.getParameter("marge")));
				toadd.setNombreRayon(Integer.valueOf(request.getParameter("nombreRayon")));
				toadd.setNombreStock(Integer.valueOf(request.getParameter("nombreStock")));
			
					
				//auto generated when we add
				//toadd.setIdProduit(Integer.valueOf(request.getParameter("idProduit")));
				//FIXME
				//toadd.setIdRayon(Integer.valueOf(request.getParameter("idRayon")));
				toadd.setPrixHt(Integer.valueOf(request.getParameter("prixHT")));
				toadd.setPrixTTC(Integer.valueOf(request.getParameter("prixTTC")));
				toadd.setQuantity(Integer.valueOf(request.getParameter("quantity")));
				toadd.setSeuilMiniRayon(Integer.valueOf(request.getParameter("seuilMiniRayon")));
				toadd.setSeuilMiniStock (Integer.valueOf(request.getParameter("seuilMiniStock")));
				toadd.setTVA(Integer.valueOf(request.getParameter("TVA")));
				
				session.save(toadd);
				tx.commit();
				return mapping.findForward("Success");	
			}	
}
